package org.androidpn.fragment;

import org.androidpn.R;
import org.androidpn.activity.ModifyUserInfoActivity;
import org.androidpn.http.HttpApi;
import org.androidpn.listener.CommentResponseListener;
import org.androidpn.model.CUserInfo;
import org.androidpn.utils.AppCache;
import org.androidpn.utils.ConstanceUtil;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment4 extends Fragment implements OnClickListener {
	private ImageView ivHead;
	private TextView tvPhone;
	private TextView tvName;
	private TextView tvAge;
	private TextView tvSex;
	private TextView tvHospitalTextView;
	private TextView tvMajor;
	private TextView tvDescription;
	private TextView tvTag;
	private CUserInfo cUserInfo;

	private RelativeLayout lyHospital;
	private RelativeLayout lyMarjor;
	private RelativeLayout lyDscription;
	private RelativeLayout lyHead;
	private RelativeLayout lyPhone;
	private RelativeLayout lyName;
	private RelativeLayout lyAge;
	private RelativeLayout lySex;
	private RelativeLayout rlTag;
	private TextView tvLogout;

	private final String IMAGE_TYPE = "image/*";
	public final static int IMAGE_CODE = 0; // 这里的IMAGE_CODE是自己任意定义的
	public final static int MODIFY_CODE = 7;

	public Fragment4() {
		// Required empty public constructor
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		View view = inflater.inflate(R.layout.fragment_fragment4, container, false);
		ivHead = (ImageView) view.findViewById(R.id.iv_head);
		tvPhone = (TextView) view.findViewById(R.id.tv_phone);
		tvName = (TextView) view.findViewById(R.id.tv_name);
		tvAge = (TextView) view.findViewById(R.id.tv_age);
		tvSex = (TextView) view.findViewById(R.id.tv_sex);
		tvHospitalTextView = (TextView) view.findViewById(R.id.tv_hospital);
		tvDescription = (TextView) view.findViewById(R.id.tv_description);
		tvMajor = (TextView) view.findViewById(R.id.tv_major);
		lyHospital = (RelativeLayout) view.findViewById(R.id.ly_hospital);
		lyDscription = (RelativeLayout) view.findViewById(R.id.ly_description);
		lyMarjor = (RelativeLayout) view.findViewById(R.id.ly_major);
		tvLogout=(TextView) view.findViewById(R.id.tv_logout);
		lyHead = (RelativeLayout) view.findViewById(R.id.ly_head);
		lyPhone = (RelativeLayout) view.findViewById(R.id.ly_telephone);
		lyName = (RelativeLayout) view.findViewById(R.id.ly_name);
		lyAge = (RelativeLayout) view.findViewById(R.id.ly_age);
		lySex = (RelativeLayout) view.findViewById(R.id.ly_sex);
		rlTag = (RelativeLayout) view.findViewById(R.id.ly_tag);
		tvTag = (TextView) view.findViewById(R.id.tv_tag);
		rlTag.setOnClickListener(this);
		lyHospital.setOnClickListener(this);
		lyMarjor.setOnClickListener(this);
		lyDscription.setOnClickListener(this);
		lyHead.setOnClickListener(this);
		lyPhone.setOnClickListener(this);
		lyName.setOnClickListener(this);
		lyAge.setOnClickListener(this);
		lySex.setOnClickListener(this);
		tvLogout.setOnClickListener(this);
		initData();
		return view;
	}

	public void initData() {
		cUserInfo = AppCache.getInstance().getUseInfo();
		if (cUserInfo != null) {
			DisplayImageOptions options1 = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)
					.considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
					.showImageOnFail(R.drawable.default_icon)
					.showImageForEmptyUri(R.drawable.default_icon)
					.showImageOnLoading(R.drawable.default_icon).build();
			if (!TextUtils.isEmpty(cUserInfo.getIcon())) {
				ImageLoader.getInstance().displayImage(ConstanceUtil.HOST_NAME + cUserInfo.getIcon(), ivHead, options1);
			}
			tvPhone.setText(cUserInfo.getTelephone());
			tvName.setText(cUserInfo.getName());
			tvAge.setText(cUserInfo.getAge() + "");
			tvTag.setText(cUserInfo.getTag());
			if (cUserInfo.getSex() == ConstanceUtil.TYPE_SEX_WOMAN) {
				tvSex.setText("女");
			} else {
				tvSex.setText("男");
			}
			if (cUserInfo.getType() == ConstanceUtil.TYPE_YISHENG) {
				tvHospitalTextView.setText(cUserInfo.getHospital());
				tvDescription.setText(cUserInfo.getDescription());
				tvMajor.setText(cUserInfo.getMajor());
				// String tag =new String(cUserInfo.getTag());
				// tag.replace(":", "、");

			} else {
				lyHospital.setVisibility(View.GONE);
				lyMarjor.setVisibility(View.GONE);
				lyDscription.setVisibility(View.GONE);
			}
		}
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(getActivity(), ModifyUserInfoActivity.class);
		intent.putExtra("cuserinfo", cUserInfo);
		switch (arg0.getId()) {
		case R.id.ly_hospital:
			intent.putExtra("modifytype", ConstanceUtil.MODIFY_TYPE_HOSPITAL);
			startActivityForResult(intent, MODIFY_CODE);
			break;
		case R.id.ly_major:
			intent.putExtra("modifytype", ConstanceUtil.MODIFY_TYPE_MAJOR);
			startActivityForResult(intent, MODIFY_CODE);
			break;
		case R.id.ly_head:
			Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
			getAlbum.setType(IMAGE_TYPE);
			getActivity().startActivityForResult(getAlbum, IMAGE_CODE);
			break;
		case R.id.ly_description:
			intent.putExtra("modifytype", ConstanceUtil.MODIFY_TYPE_DESCRIPTION);
			startActivityForResult(intent, 1000);
			break;
		case R.id.ly_name:
			intent.putExtra("modifytype", ConstanceUtil.MODIFY_TYPE_NAME);
			startActivityForResult(intent, MODIFY_CODE);
			break;
		case R.id.ly_age:
			intent.putExtra("modifytype", ConstanceUtil.MODIFY_TYPE_AGE);
			startActivityForResult(intent, MODIFY_CODE);
			break;
		case R.id.ly_sex:
			intent.putExtra("modifytype", ConstanceUtil.MODIFY_TYPE_SEX);
			startActivityForResult(intent, MODIFY_CODE);
			break;
		case R.id.ly_tag:
			intent.putExtra("modifytype", ConstanceUtil.MODIFY_TYPE_TAG);
			startActivityForResult(intent, MODIFY_CODE);
			break;
		case R.id.tv_logout:
			CommentResponseListener listener =new CommentResponseListener ();
			HttpApi.unRegisterDevice(cUserInfo.getTelephone(),listener , listener);
			getActivity().finish();
			break;

		}
	}

	@Override
	public void onResume() {
		initData();
		super.onResume();
	}
}
